package com.peas.platform.module.icims.base.auth.domain.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.auth.adapter.repository.RoleRepository;
import com.peas.platform.module.icims.base.auth.adapter.repository.po.RolePo;
import com.peas.platform.module.icims.base.auth.domain.RoleDomain;
import com.peas.platform.module.icims.base.auth.dto.RoleDto;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class RoleDomainImpl extends BaseDomainImpl<RoleDto, RolePo> implements RoleDomain {

    @Resource
    private RoleRepository roleRepository;
    @Resource
    private SequenceService sequenceService;

    @Override
    public BaseRepository<RolePo> getMapper() {
        return roleRepository;
    }

    @Override
    public Class<? extends BaseRepository<RolePo>> getRepositoryClass() {
        return RoleRepository.class;
    }

    @Transactional
    @Override
    public int save(RoleDto dto) {
        if (StrUtil.isBlank(dto.getRoleCode())) {
            String code = sequenceService.genSequence("base", "RoleDto", "RD", 4, 1);
            dto.setRoleCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("roleCode", dto.getRoleCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(RoleDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("roleCode", dto.getRoleCode());
        return result;
    }

}
